# import time
from itsdangerous import TimedJSONWebSignatureSerializer as Serializer, BadData
from django.conf import settings

# SECRET_KEY = 'vd1sha1u)&d4h-rzitjh8_f))y@4rip$&du%a=bm$7&p0m%@&_'
# # serializer = Serializer(秘钥, 有效期秒)
# serializer = Serializer(SECRET_KEY, 3)
# # serializer.dumps(数据), 返回bytes类型
# d = {'openid': '18512345678'}
# print(d)
# token = serializer.dumps(d)
# token = token.decode()
# print(token)
#
# # 检验token
# # 验证失败，会抛出itsdangerous.BadData异常
# serializer = Serializer(SECRET_KEY, 3)
# try:
#     data = serializer.loads(token)
# except BadData:
#     print('bad_data')
# print(data)




def encode_signature_openid(openid):
    serializer = Serializer(settings.SECRET_KEY, 300)
    token = serializer.dumps({"openid": openid})
    return token.decode()


def decode_signature_openid(token):
    serializer = Serializer(settings.SECRET_KEY, 300)
    try:
        data = serializer.loads(token)
    except Exception as e:
        return None
    return data['openid']
